//
//  UIViewController+ZSTopDownHUD.m
//  ZSTopDownHUD.h ZSTopDownHUD.m ZSTopDownView.h ZSHUDSet-master
//
//  Created by safiri on 2018/6/29.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UIViewController+ZSTopDownHUD.h"
#import <objc/runtime.h>

@implementation UIViewController (ZSTopDownHUD)

- (void)showTDSuccessHUDAutoHide:(NSString *)text {
    [self showTDHUDAutoHideWithType:HUDViewTypeSuccess text:text];
}
- (void)showTDWarningHUDAutoHide:(NSString *)text {
    [self showTDHUDAutoHideWithType:HUDViewTypeWarning text:text];
}
- (void)showTDErrorHUDAutoHide:(NSString *)text {
    [self showTDHUDAutoHideWithType:HUDViewTypeError text:text];
}
- (void)showTDMessageHUDAutoHide:(NSString *)text {
    [self showTDHUDAutoHideWithType:HUDViewTypeMessage text:text];
}
- (void)showTDNetHUDAutoHide:(NSString *)text {
    [self showTDHUDAutoHideWithType:HUDViewTypeNetStatus text:text];
}
- (void)showTDHUDAutoHideWithType:(HUDViewType)type text:(NSString *)hintText {
    [self showTDHUDAutoHideWithType:type text:hintText statusBarChangeStyle:ChangeDefaultToLightContent];
}

- (void)showTDHUDAutoHideWithType:(HUDViewType)type text:(NSString *)hintText statusBarChangeStyle:(StatusBarChangeStyleForTDHUD)changeStyle {
    [self showTDHUDAutoHideWithType:type image:nil hint:hintText hintTextColor:nil backgroundColor:nil statusBarChangeStyle:changeStyle];
}

- (void)showTDHUDAutoHideWithType:(HUDViewType)type image:(nullable UIImage *)hintImage hint:(NSString *)hintText hintTextColor:(nullable UIColor *)textColor backgroundColor:(UIColor *)backgroundColor statusBarChangeStyle:(StatusBarChangeStyleForTDHUD)changeStyle {
    if ([hintText isKindOfClass:[NSString class]]) {
        if (changeStyle == ChangeDefaultToLightContent) {
            self.statusBarStyleForTopDownHUD = UIStatusBarStyleDefault;
        }else {
            self.statusBarStyleForTopDownHUD = UIStatusBarStyleLightContent;
        }
        [[ZSTopDownHUD shareHUD] showHUDWithType:type image:hintImage hint:hintText hintTextColor:textColor backgroundColor:backgroundColor];
        __weak typeof(self) weakSelf = self;
        [[ZSTopDownHUD shareHUD] hideHUDWithTimeIntervalToHide:0.7 completion:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (changeStyle == ChangeDefaultToLightContent) {
                strongSelf.statusBarStyleForTopDownHUD = UIStatusBarStyleLightContent;
            }else {
                strongSelf.statusBarStyleForTopDownHUD = UIStatusBarStyleDefault;
            }
        }];
    }
}

/// 配合HUD改变UIStatusBarStyle
- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.statusBarStyleForTopDownHUD;//childViewControllerForStatusBarStyle 返回nil才起作用
}
- (UIViewController *)childViewControllerForStatusBarStyle {
    return nil;
}
- (void)setStatusBarStyleForTopDownHUD:(UIStatusBarStyle)statusBarStyleForTopDownHUD {
    objc_setAssociatedObject(self, @selector(statusBarStyleForTopDownHUD), @(statusBarStyleForTopDownHUD), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)statusBarStyleForTopDownHUD {
    NSNumber *style = objc_getAssociatedObject(self, _cmd);
    return style.integerValue;
}

@end
